<template>
  <div class="main-container">
    <div class="item-content">
      <div class="form-content" style="margin-bottom: 20px">
        <div class="item-header">
          <el-upload
            :on-success="trainingSuccess"
            action="/api/upload"
            :on-error="fileError"
            :show-file-list="false"
          >
            <el-button type="primary"
              >上传文件<i class="el-icon-upload el-icon--right"></i
            ></el-button>
          </el-upload>
          <div></div>
        </div>
        <div>
          <el-table
            height="500"
            v-loading="loading"
            :data="trainigFileTableData"
            border
            style="width: 100%"
          >
            <template #empty>
              <el-empty description="暂无数据"> </el-empty>
            </template>
            <el-table-column prop="createName" label="创建者名">
            </el-table-column>
            <el-table-column prop="fileName" label="文件名"> </el-table-column>
            <el-table-column prop="createTime" label="创建时间">
              <template #default="{ row }">
                <el-date-picker
                  v-model="row.createTime"
                  type="date"
                  disabled
                  placeholder="选择日期"
                >
                </el-date-picker>
              </template>
            </el-table-column>
            <el-table-column label="操作" fixed="right">
              <template #default="{ row }">
                <el-popconfirm
                  style="margin-right: 10px"
                  title="确定删除吗？"
                  @confirm="deleteRow(row.id)"
                >
                  <el-link type="primary" slot="reference">删除</el-link>
                </el-popconfirm>
                <el-link
                  type="primary"
                  :href="'/#/pdf?address=' + row.fileUrl"
                  target="blank"
                  :key="row.id"
                >
                  查看</el-link
                >
              </template>
            </el-table-column>
          </el-table>
        </div>
      </div>
      <div
        style="and
          width: 100%;
          display: flex;
          justify-content: center;
          align-items: center;
          height: 50px;
          margin: 0;
        "
      >
        <el-pagination
          :current-page="page"
          :page-sizes="[10, 15, 50, 100, 200, 300, 400, 1000]"
          :page-size="size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        >
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script setup>
import { defineComponent } from "vue";

const NewComponentName = defineComponent({
  name: "NewComponentName",
});
console.log(NewComponentName);
import { onBeforeMount, ref } from "vue";
import { addTraining, searchTraining, deleteTraining } from "@/api/training";
// searchTrainingTotal
const total = ref(0);
const page = ref(1);
const size = ref(10);
import { Notification } from "element-ui";
const loading = ref(true);
const changeTable = async () => {
  loading.value = true;
  let result = await searchTraining(page.value, size.value);
  trainigFileTableData.value = result?.data;
  total.value = Number(result?.message);
  if (isNaN(total.value)) {
    total.value = 0;
  }
  loading.value = false;
};
const deleteRow = async (id) => {
  let result = await deleteTraining(id);
  if (result.code == 0) {
    Notification({
      title: "成功",
      message: "删除成功",
      type: "success",
    });
    changeTable();
  } else {
    Notification({
      title: "失败",
      message: "删除失败",
      type: "warning",
    });
  }
};
const trainingSuccess = async (res) => {
  if (res?.code == 0) {
    let result = await addTraining({
      fileName: res.data[1],
      fileUrl: "api/" + res.data[0],
    });
    if (result?.code == 0) {
      Notification({
        title: "成功",
        message: "上传成功",
        type: "success",
      });
      changeTable();
    } else {
      Notification({
        title: "失败",
        message: "上传失败",
        type: "warning",
      });
    }
  } else {
    Notification({
      title: "失败",
      message: "上传失败",
      type: "warning",
    });
  }
};

const handleSizeChange = async (val) => {
  size.value = val;
  changeTable();
};
const handleCurrentChange = async (val) => {
  page.value = val;
  changeTable();
};
onBeforeMount(async () => {
  changeTable();
});

const fileError = () => {
  Notification({
    title: "失败",
    message: "上传失败",
    type: "warning",
  });
};

const trainigFileTableData = ref([]);
</script>
<style lang="scss" scoped>
:deep(.el-icon-arrow-right:before) {
  font-size: 12px !important;
}
:deep(.el-table th) {
  background: #fafafa;
}
:deep(.el-collapse-item__header) {
  font-weight: bold;
  color: #333;
  font-size: 15px;
  padding: 0 10px;
}
:deep(.el-icon-arrow-right:before) {
  font-weight: bold;
  color: #333;
  font-size: 25px;
}
// :deep(.cell) {
//   display: flex;
//   justify-content: space-around;
// }
/*去除upload组件过渡效果*/
::v-deep .el-upload-list__item {
  transition: none !important;
}
::v-deep .el-upload-list__item-thumbnail {
  /* 图片在方框内显示长边 */
  object-fit: scale-down !important;
}

:deep(.el-input__inner) {
  height: auto !important;
}
:deep(.el-icon-arrow-right:before) {
  font-size: 12px !important;
}
:deep(.el-table th) {
  background: #fafafa;
}
:deep(.el-collapse-item__header) {
  font-weight: bold;
  color: #333;
  font-size: 15px;
  padding: 0 10px;
}
:deep(.el-icon-arrow-right:before) {
  font-weight: bold;
  color: #333;
  font-size: 25px;
}
body {
  min-height: 100vh;

  transition: all 0.3s ease;
}
.container {
  height: 100%;
  padding: 15px;
  overflow: auto;
  box-sizing: border-box;

  .main-content {
    background-color: #fff;
    border: 1px solid #e6e6e6;
    border-radius: 6px;
    min-width: 700px;

    .header-container {
      padding: 15px 15px;
      border-bottom: solid 1px #e6e6e6;
      box-sizing: border-box;
      display: flex;
      flex-direction: row;
      align-items: center;
      justify-content: space-between;
      flex-wrap: wrap;

      .title {
        position: relative;
        font-weight: bold;
        color: #333;
        font-size: 15px;
        padding-left: 16px;

        &::before {
          content: "";
          width: 3px;
          height: 16px;
          background-color: #82a848;
          left: 5px;
          top: calc(50% - 8px);
          position: absolute;
        }

        &::after {
          content: "";
          width: 3px;
          height: 16px;
          background-color: #82a848;
          left: 5px;
          top: calc(50% - 8px);
          position: absolute;
        }
      }
    }
  }
}

.main {
  padding: 12px 15px;

  .item-content {
    height: 100%;
    overflow: hidden;
    border-radius: 4px;
    box-sizing: border-box;
    background-color: #fff;
    display: flex;
    flex-direction: column;
    margin-bottom: 20px;

    .item-header {
      display: flex;
      flex-direction: row;
      align-items: center;
      justify-content: space-between;
      margin-bottom: 10px;

      .subheading {
        position: relative;
        color: #555;
        font-size: 14px;
        padding-left: 14px;

        &::after {
          content: "";
          width: 2px;
          height: 14px;
          background-color: #82a848;
          left: 0;
          top: calc(50% - 7px);
          position: absolute;
        }

        &::before {
          content: "";
          width: 2px;
          height: 14px;
          background-color: #82a848;
          left: 4px;
          top: calc(50% - 7px);
          position: absolute;
        }
      }
    }

    .content-container {
      margin-top: -1px;
    }
  }
}

.w-100 {
  width: 100%;
}

.item-form-content {
  display: flex;
  flex-direction: row;
  align-items: center;

  i {
    margin: 0 10px;
  }
}

.productList-content {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
  height: 40px;
}

.form-content {
  padding: 0 10px;
}

.dialog-action-bar {
  text-align: center;
  margin-top: 30px;
}
</style>
